package com.zlx.mall.basic.util.json;

import java.lang.reflect.Type;

/**
 * @author qingke
 **/
public class JsonDeserializationException extends JsonRuntimeException {

    private static final String DEFAULT_MESSAGE = "Json util deserialize failed. ";

    private static final String MESSAGE_FOR_SPECIFIED_CLASS = "Json util deserialize for class [%s] failed. ";

    private static final String ERROR_MESSAGE_FOR_SPECIFIED_CLASS = "Json util deserialize for class [%s] failed, cause error[%s]. ";

    private Class<?> targetClass;

    public JsonDeserializationException(Class<?> targetClass) {
        super(String.format(MESSAGE_FOR_SPECIFIED_CLASS, targetClass.getName()));
        this.targetClass = targetClass;
    }

    public JsonDeserializationException(Type targetType) {
        super(String.format(MESSAGE_FOR_SPECIFIED_CLASS, targetType.toString()));
    }

    public JsonDeserializationException(Throwable throwable) {
        super(DEFAULT_MESSAGE, throwable);
    }

    public JsonDeserializationException(Class<?> targetClass, Throwable throwable) {
        super(String.format(ERROR_MESSAGE_FOR_SPECIFIED_CLASS, targetClass.getName(), throwable.getMessage()), throwable);
        this.targetClass = targetClass;
    }

    public JsonDeserializationException(Type targetType, Throwable throwable) {
        super(String.format(ERROR_MESSAGE_FOR_SPECIFIED_CLASS, targetType.toString(), throwable.getMessage()), throwable);
    }

    public Class<?> getTargetClass() {
        return targetClass;
    }
}
